import stateman from "../components/router/router.js";

const login = ()=>{
    $.ajax({
        url: "web/club/login/"
    }).done(function(){
        console.log("Login Success");
    }).fail(function(){
        console.error("Login Fail");
    });
};

const getClubInfo = (id) => {
    return new Promise(function(resolve, reject){
        
        $.ajax({
            url: "web/club/" + id + "/getClubInfo/"
        }).done(function(res){
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                console.log(res);
                resolve(res);
            }
        }).fail(function(err){
            reject(err);
        });

    });
};

const getDepts = (id) => {
    console.log(id);
    return new Promise((resolve, reject)=>{

        $.ajax({
            url: "web/club/" + id + "/getDepts/"
        }).done(function(res){
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data.depts);
            }
        }).fail(function(err){
            reject(err);
        });
    });
};

const getDefaultActivities = (club_id)=>{
    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/" + club_id + "/getDefaultActivities/",
        }).done(function(res){
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data.activities);
            }
        }).fail(function(err){
            reject(err);
        });
    });
};

const getClubs = function(){
    return new Promise((resolve, reject)=>{

        $.ajax({
            url: "web/club/getClubs/"
        }).done(function(res){
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data.clubs);
            }
        }).fail(function(err){
            console.error(err);
            reject(err);
        });
    });
};

const putHonor = function(club_id, year, honor){
    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/putHonor/",
            data: JSON.stringify({
                club_id: club_id,
                year: year,
                title: honor
            }),
            contentType: "application/json",
            dataType: "json",
            method:"post",
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const delHonor = function(club_id, honor_id){
    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/delHonor/",
            data: JSON.stringify({
                club_id: club_id,
                honor_id: honor_id
            }),
            contentType: "application/json",
            dataType: "json",
            method:"post",
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const updateClubInfo = (info)=>{
    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/updateClubInfo/",
            data: JSON.stringify(info),
            contentType: "application/json",
            dataType: "json",
            method:"post",
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const delDept = (club_id, dept_id)=>{

    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/delDept/",
            data: JSON.stringify({
                club_id: club_id,
                dept_id: dept_id
            }),
            contentType: "application/json",
            dataType: "json",
            method:"post",
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const delDefaultActivity = (club_id, default_activity_id)=>{

    stateman.emit("ACCESS_DENYED");

    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/delDefaultActivity/",
            data: JSON.stringify({
                club_id: club_id,
                default_activity_id: default_activity_id
            }),
            contentType: "application/json",
            dataType: "json",
            method:"post",
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const getClubLogo = (club_id)=>{
    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/" + club_id +"/getClubLogo/"
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const delClubImage = (club_id, club_image_id)=>{
    return new Promise((resolve, reject)=>{
        $.ajax({
            url: "web/club/delClubImage/",
            data: JSON.stringify({
                club_id: club_id,
                club_image_id: club_image_id
            }),
            contentType: "application/json",
            dataType: "json",
            method:"post",
        }).done((res)=>{
            var next = preprocessRes(res);
            if(next){
                reject(res);
                next();
                return;
            }
            if(res.status != 200){
                reject(res);
            }else{
                resolve(res.data);
            }
        }).fail((err)=>{
            reject(err);
        });
    });
};

const preprocessRes = (res)=>{
    if(res.status == 500){
        return function(){
            alert("服务异常");
        };
    }else if(res.status == 100){
        return function(){
            alert("无权访问");
            stateman.emit("ACCESS_DENYED");
        };
    }
};

export {
    login,
    getClubInfo,
    getClubs,
    getDepts,
    getDefaultActivities,
    putHonor,
    delHonor,
    updateClubInfo,
    delDept,
    delDefaultActivity,
    getClubLogo,
    delClubImage
};